<template>
  <div class="container">
    <div class="header"></div>
    <div class="login">
      <div class="mainbox">
        <div class="loginbox">
          <h2>平台登录</h2>
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm input"
          >
            <el-form-item label="账号" prop="name">
              <el-input
                placeholder="请输入账户名"
                v-model="ruleForm.name"
              ></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input
                placeholder="请输入密码"
                v-model="ruleForm.password"
                show-password
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm('ruleForm')"
                >登录</el-button
              >
              <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: "Login",
  data() {
    return {
      // input: "",
      ruleForm: {
        name: "",
        password: "",
      },
      rules: {
        name: [{ required: true, message: "请输入账户名", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var str = `username=${this.ruleForm.name}&password=${this.ruleForm.password}`;
          this.axios.post("login", str).then((res) => {
            console.log(res);
            if (res.data.meta.status == 200) {
              this.$message("登录成功");
              sessionStorage.setItem("token", res.data.data.token);
              this.$router.push("/");
            } else {
              this.$message(res.data.meta.msg);
            }
          });
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
<style lang="scss" scoped>
.container {
  @mixin size($w, $h) {
    width: $w;
    height: $h;
  }
  $color: #fff;

  height: 100%;
  .header {
    @include size(100%, 100px);
    }
  .login {
    background: #2980b6 url("../assets/map.jpg") no-repeat center center;
    .mainbox {
      @include size(960px, 360px);
      margin: 0px auto;
      padding-top: 40px;
      .loginbox {
        @include size(260px, 280px);
        background-color: $color;
        float: right;
        padding: 20px;
        h2 {
          font-size: 18px;
        }
        .input {
          margin-left: -50px;
        }
        .btn {
          width: 260px;
          background: #3fb3b2;
          margin-top: 20px;
          color: $color;
        }
      }
    }
  }
}
</style>